package com.agileai.bpm.controller.emulation;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.agileai.bpm.bizmoduler.emulation.WfProcessHistoryListQuery;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.controller.core.QueryModelListHandler;
import com.agileai.hotweb.domain.FormSelect;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.RedirectRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.DateUtil;
import com.agileai.util.StringUtil;

public class WfProcessHistoryListQueryHandler extends QueryModelListHandler{
	public WfProcessHistoryListQueryHandler(){
		super();
		this.detailHandlerClazz = TaskApproveFrameHandler.class;
		this.serviceId = buildServiceId(WfProcessHistoryListQuery.class);
	}
	public ViewRenderer prepareDisplay(DataParam param){
		try {
			mergeParam(param);
			initParameters(param);
			param.put("hisStyle", param.getString("HIS_STYLE"));
			this.setAttributes(param);
			List<DataRow> rsList = new ArrayList<DataRow>();
			User user = (User)this.getUser();
			String userId = param.get("userId");
			if (user.isAdmin()){
				rsList = getService().findRecords(param);
				setAttribute("embed", true);
			}else if(user.isAdmin()&&StringUtil.isNotNullNotEmpty(userId)){
				param.put("userId",userId);
				rsList = getService().findRecords(param);
			}else{
				param.put("userId",user.getUserId());
				rsList = getService().findRecords(param);
			}
			processPageAttributes(param);
			this.setRsList(rsList);
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new LocalRenderer(getPage());
	}
	public ViewRenderer doRedirectAction(DataParam param){
		storeParam(param);
		String url = param.get("bizFormURL");
		url = url+"&comeFromTaskList=true";
		return new RedirectRenderer(url);
	}
	
	protected void processPageAttributes(DataParam param) {
		setAttribute("HIS_STYLE", FormSelectFactory.create("HIS_STYLE")
				.addSelectedValue(getAttributeValue("HIS_STYLE", "HIS_PROCESS")));
		List<DataRow> records = getService().findProcessRecords(param);
		FormSelect formSelect = new FormSelect();
		formSelect.setKeyColumnName("WFP_CODE");
		formSelect.setValueColumnName("WFP_NAME");
		formSelect.putValues(records);
		setAttribute("wfpName",formSelect.addSelectedValue(param.get("wfpName")));
		initMappingItem("WFP_NAME",formSelect.getContent());
	}
	protected void initParameters(DataParam param) {
		initParamItem(param, "HIS_STYLE", "HIS_PROCESS");
        initParamItem(param,"sdate",DateUtil.getDateByType(DateUtil.YYMMDD_HORIZONTAL,DateUtil.getDateAdd(DateUtil.getBeginOfMonth(new Date()),DateUtil.MONTH,-1)));
 		initParamItem(param,"edate",DateUtil.getDateByType(DateUtil.YYMMDD_HORIZONTAL,DateUtil.getDateAdd(new Date(), DateUtil.DAY, 1)));
	}
	protected WfProcessHistoryListQuery getService() {
		return (WfProcessHistoryListQuery)this.lookupService(this.getServiceId());
	}
}	
